package xml;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import cuShapes.StraightLine;
import cuShapes.ArcLine;
import cuShapes.PQLine;
import java.util.List;
import java.util.ArrayList;

/**
 * Switches between a java ready list of {@link cuShapes.PQLine PQLines}, and a {@link xml.LineListType LineListType} object needed to write to xml.
 * @author mylesc,varunr
 *
 */
public class LinesAdapter extends XmlAdapter<LineListType, List<PQLine>> {
	
	/**
	 * Marshals a java list of PQLines into a LineListType object which is capable of being saved in xml.
	 * @param lines a list of PQLines to be converted into xml saveable format.
	 * @return LinesListType an object ready to be saved to xml.
	 */
	public LineListType marshal(List<PQLine> lines) throws Exception {
		List<StraightLine> straightLines = new ArrayList<StraightLine>();
		List<ArcLine> arcLines = new ArrayList<ArcLine>();

		for (PQLine e : lines) {
			if (e instanceof StraightLine) {
				straightLines.add((StraightLine) e);
			} else if (e instanceof ArcLine){
				arcLines.add((ArcLine) e);
			}
		}
		return new LineListType(straightLines, arcLines);
	}

	/**
	 * Unmarshals the LinesListType form of the list of PQLines and creates a new java ready list of PQLines.
	 * @param lineList a LinesListType object that contains all data read in from xml.
	 * @return lines a list of PQLines created from the saved data in linesList.
	 */
	public List<PQLine> unmarshal(LineListType lineList) throws Exception { 
		List<PQLine> lines = new ArrayList<PQLine>();
        lines.addAll(lineList.getStraightLines());
        lines.addAll(lineList.getArcLines());
        
        return lines;
	}
}
